﻿using System;
using Manso.cBot.Common;
using VhaBot.Common;

namespace Manso.cBot.DefaultModules.Core
{
    public class HelpCommand : ICommand
    {
        private readonly ListenToEnum _listenTo;
        private readonly string _command;
        private readonly string _help;
        private readonly string _usage;
        private readonly AccessControlLevel _accessLevel;

        public HelpCommand()
        {
            _listenTo = ListenToEnum.Both;
            _command = "Help";
            _usage = "help [command_name]";
            _help = "Shows help information";
            _accessLevel = AccessControlLevel.Guest;
        } 
        
        public ListenToEnum ListenTo
        {
            get { return _listenTo; }
        }

        public string Command
        {
            get { return _command; }
        }

        public string Help
        {
            get { return _help; }
        }

        public string Usage
        {
            get { return _usage; }
        }

        public AccessControlLevel AccessLevel
        {
            get { return _accessLevel; }
        }

        public void ExecuteCommand(string[] args, string executedBy, ListenToEnum listenTo)
        {
            if (args.Length > 0 && args[0].Length > 0)
            {
                VerboseHelp(executedBy, args[0], listenTo);
            }
            else
            {
                DefaultHelp(executedBy);
            }
        }

        private static void VerboseHelp(string executedBy, string p, ListenToEnum listenTo)
        {
            ICommand cmd = cBotContext.Instance.ModuleManager.GetCommand(p, listenTo);
            if (cmd == null)
            {
                cBotContext.Instance.SendPrivateMessage(executedBy, "Help: Command not found");
            }
            else
            {
                cBotContext.Instance.SendPrivateMessage(executedBy, "Usage: " + cmd.Usage);
            }

        }

        private static void DefaultHelp(String executedBy)
        {
            TextWindow textWindow = new TextWindow();
            textWindow.AppendString("cBot by Broker (brok3r@gmail.com)");
            textWindow.AppendLineBreak(2);

            foreach (IModule module in cBotContext.Instance.ModuleManager.Modules)
            {                
                textWindow.AppendColorString("FF0000", module.Name);
                textWindow.AppendLineBreak();
                textWindow.AppendString("Author: " + module.Author);
                textWindow.AppendLineBreak();
                textWindow.AppendString(module.Description);
                textWindow.AppendLineBreak();
                foreach (ICommand command in module.Commands)
                {
                    if (cBotContext.Instance.AccessControl.IsAllowed(executedBy, command.AccessLevel))
                    {
                        textWindow.AppendColorString("0000FF", command.Command);
                        textWindow.AppendString(": " + command.Help);
                        textWindow.AppendLineBreak();
                    }
                }
                textWindow.AppendLineBreak();
                textWindow.AppendLineBreak();
            }
            cBotContext.Instance.SendPrivateMessage(executedBy, textWindow.ToString("Click here for help"));
        }        
    }
}
